﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WC.DL.DALC.IF;
using WC.BL.BE;
using SAP.Middleware.Connector;

namespace WC.DL.DALC.SAP
{
    public class ClienteDALC: IClienteDALC
    {
        #region Singleton
        public static ClienteDALC Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (typeof(ClienteDALC))
                    {
                        if (_instance == null)
                        {
                            _instance = new ClienteDALC();
                        }
                    }
                }
                return _instance;
            }
        }

        private ClienteDALC() { }
        private static volatile ClienteDALC _instance = null;
        #endregion


        #region IClienteDALC Members

        public List<ClienteBE> find(string pCodigo, string pDescripcion, string pRuc)
        {
            List<ClienteBE> lstClientes = new List<ClienteBE>();

            ClienteBE objCliente = null; RfcDestination dest;
            IRfcFunction function = null;
            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_002");
                function.SetValue("I_KUNNR", pCodigo);
                function.SetValue("I_NAME1", pDescripcion);
                function.SetValue("I_STCD1", pRuc);
                function.Invoke(dest);

                IRfcTable T_HELP_KNA1 = function.GetTable("T_HELP_KNA1");
                foreach (IRfcStructure S_HELP_KNA1 in T_HELP_KNA1)
                {
                    objCliente = new ClienteBE();
                    objCliente.Codigo = S_HELP_KNA1["KUNNR"].GetString();
                    objCliente.Nombre = S_HELP_KNA1["NAME1"].GetString();// +E_KNA1["NAME2"].GetString() + E_KNA1["NAME3"].GetString();
                    objCliente.Ramo = S_HELP_KNA1["BRSCH"].GetString();
                    objCliente.RamoDesc = S_HELP_KNA1["BRTXT"].GetString();
                    objCliente.Correo = S_HELP_KNA1["SMTP_ADDR"].GetString();
                    objCliente.Ruc = S_HELP_KNA1["STCD1"].GetString();

                    lstClientes.Add(objCliente);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstClientes;
        }

        public ClienteBE read(string pRuc)
        {
            ClienteBE objCliente = null; RfcDestination dest;
            IRfcFunction function = null;
            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_004");
                function.SetValue("I_KUNNR", pRuc);
                function.Invoke(dest);

                IRfcStructure E_KNA1 = function.GetStructure("E_KNA1");
                if (E_KNA1 != null)
                {
                    objCliente = new ClienteBE();
                    objCliente.Codigo = E_KNA1["KUNNR"].GetString();
                    objCliente.Nombre = E_KNA1["NAME1"].GetString();// +E_KNA1["NAME2"].GetString() + E_KNA1["NAME3"].GetString();
                    objCliente.Ramo = E_KNA1["BRSCH"].GetString();
                    objCliente.RamoDesc = E_KNA1["BRTXT"].GetString();
                    objCliente.Correo = E_KNA1["SMTP_ADDR"].GetString();
                    objCliente.Ruc = E_KNA1["STCD1"].GetString();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return objCliente;
        }

        public string validaCli(string pRuc)
        {
            string RUC = string.Empty;
            string retval = string.Empty;
            IRfcFunction function = null;
            RfcDestination dest;
            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_010");
                function.SetValue("I_STCD1", pRuc);
                function.Invoke(dest);

                retval = function.GetValue("E_RETURN").ToString();
            }
            catch (Exception ex)
            {
                //throw ex;
                retval = ex.Message;
            }

            return retval;
        }

        public string write(ClienteBE pObjClienteBE)
        {
            string retval = string.Empty;

            IRfcFunction function = null;
            RfcDestination dest;
            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_003");

                IRfcStructure I_TSD0003 = function.GetStructure("I_TSD0003");
                I_TSD0003["KUNNR"].SetValue(pObjClienteBE.Ruc);
                I_TSD0003["NAME1"].SetValue(pObjClienteBE.Nombre);
                I_TSD0003["STCD1"].SetValue(pObjClienteBE.Ruc);
                I_TSD0003["DIREC"].SetValue(pObjClienteBE.Direccion);
                I_TSD0003["BLAND"].SetValue(pObjClienteBE.Departamento);
                I_TSD0003["PROV"].SetValue(pObjClienteBE.Provincia);
                I_TSD0003["DIST"].SetValue(pObjClienteBE.Distrito);
                I_TSD0003["TELF1"].SetValue(pObjClienteBE.Telefono);
                I_TSD0003["TELFX"].SetValue(pObjClienteBE.Fax);
                I_TSD0003["BRSCH"].SetValue(pObjClienteBE.Ramo);
                I_TSD0003["WERKS"].SetValue(pObjClienteBE.Tienda);
                I_TSD0003["SMTP_ADDR"].SetValue(pObjClienteBE.Correo);
                //Falta responsable de compras, representante legal y dni del rep legal
                function.SetValue("I_TSD0003", I_TSD0003);
                function.Invoke(dest);

                IRfcStructure E_RETURN = function.GetStructure("E_RETURN");
                if (E_RETURN != null)
                {
                    retval = E_RETURN["MESSAGE"].GetString();
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                retval = ex.Message;
            }

            return retval;
        }

        #endregion

    }
}
